package com.zahb.qadx.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zahb.qadx.R;
import com.zahb.qadx.model.MainFunc1;
import com.zahb.qadx.model.MainFunc2;
import com.zahb.qadx.ui.activity.AllLecturerActivity;
import com.zahb.qadx.ui.activity.AttentionDetailActivity;
import com.zahb.qadx.ui.view.decoration.MColorDrawable;
import com.zahb.qadx.ui.view.decoration.MDividerItemDecoration;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.ImageLoaderKt;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by Zdw on 2020/10/10 15:47
 * 讲师
 */
public class StarTeacherFragment extends HomeFuncFragment {

    static final String TAG = "StarTeacherFragment";

    BaseQuickAdapter<MainFunc2, BaseViewHolder> mAdapter;

    static StarTeacherFragment newInstance(MainFunc1 mainFunc1) {
        Bundle args = new Bundle();
        args.putSerializable("data", mainFunc1);
        StarTeacherFragment fragment = new StarTeacherFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.ic_star_teacher, 0, 0, 0);
        MainFunc1 mainFunc1 = (MainFunc1) mArguments.getSerializable("data");
        if (mainFunc1 != null) {
            mTvTitle.setText(mainFunc1.getFunctionName());
            List<MainFunc2> mainFunc2s = mainFunc1.getInfos();
//            mAdapter.addData(mainFunc2s.size() > 3 ? mainFunc2s.subList(0, 3) : mainFunc2s);//控制首页显示子布局的数量
            if (mainFunc2s!=null){
                mAdapter.addData(mainFunc2s);
            }

        }
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
    }

    @Nullable
    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        MDividerItemDecoration itemDecoration = new MDividerItemDecoration(getContext());
        itemDecoration.setDrawable(new MColorDrawable(CompatHelper.getColor(R.color.gray_999999)).setIntrinsicHeight(1));
        itemDecoration.setShowLast(false);
        return itemDecoration;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<MainFunc2, BaseViewHolder> getAdapter() {
        mAdapter = new StarTeacherAdapter(R.layout.item_list_star_teacher);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(getContext(), AttentionDetailActivity.class);
            intent.putExtra("name", mAdapter.getItem(position).getTitle());
            intent.putExtra("relationId", String.valueOf(mAdapter.getItem(position).getSourceId()));
            intent.putExtra("img", mAdapter.getItem(position).getIcon());
            intent.putExtra("desc", mAdapter.getItem(position).getSubTitle());
            intent.putExtra("cateName", mAdapter.getItem(position).getLabelContent());
            startActivity(intent);
        });
        return mAdapter;
    }

    @Override
    protected void seeMore() {
        AllLecturerActivity.actionStart(getContext());
    }

    public static class StarTeacherAdapter extends BaseQuickAdapter<MainFunc2, BaseViewHolder> {

        StarTeacherAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, MainFunc2 mainFunc2) {
            ImageLoaderKt.loadImageCircle(baseViewHolder.getView(R.id.iv_icon), mainFunc2.getIcon(), R.drawable.ic_default_portrait);
            baseViewHolder
                    .setText(R.id.tv_name, mainFunc2.getTitle())
                    .setText(R.id.tv_desc, CompatHelper.isEmpty(mainFunc2.getSubTitle())
                            ? "" : mainFunc2.getSubTitle());
        }
    }
}
